﻿#include "TransferProxy.h"
#include "NormalChatTransfer.h"
#include "ImageTrans.h"
#include "MovieTrans.h"

#include <iostream>
#include <stdexcept>
#include <qqml.h>
#include <QQmlEngine>
#include <QDir>

//删除文件夹
bool DelDir(const QString &path)
{
    if (path.isEmpty()){
        return false;
    }
    QDir dir(path);
    if(!dir.exists()){
        return true;
    }
    dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot); //设置过滤
    QFileInfoList fileList = dir.entryInfoList(); // 获取所有的文件信息
    foreach (QFileInfo file, fileList){ //遍历文件信息
        if (file.isFile()){ // 是文件，删除
            file.dir().remove(file.fileName());
        }else{ // 递归删除
            DelDir(file.absoluteFilePath());
        }
    }
    return dir.rmdir(dir.absolutePath()); // 删除文件夹
}

TransferProxy::TransferProxy(QTcpSocket *tcpSocket) {
    this->mTcpSocket = tcpSocket;
    mTransVec.push_back(new NormalChatTransfer(this));
    mTransVec.push_back(new ImageTrans(this));
    mTransVec.push_back(new MovieTrans(this));

    //告诉QML你别删我指针，我自己会管
    QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
}

bool TransferProxy::sendInfo(const QString &infoType, const QVariant &var, const QVariant &extra) {
    //根究infoType去寻找
    auto transPtr = getPtr(infoType);
    if(transPtr) {
        transPtr->send(var, extra);
        return true;
    }else {
        return false;
    }
}

void TransferProxy::recvInfo(const QString &recvInfo)
{
    TransferBase* basePtr = isHaveReceiving();
    //如果有传输任务在运行，直接继续运行任务直到任务结束
    if(basePtr) {
        basePtr->received(recvInfo);
        return;
    }
    //到这里说明没有任务进行，所以检查字段，字段相符再进行传输
    for(TransferBase* base : mTransVec) {
        QString infoType = base->transferPrefix() + INFOFLAG;
        qDebug() << "info type = " << infoType;
        if(recvInfo.contains(infoType, Qt::CaseInsensitive)) {
            QString ss = recvInfo;
            ss = ss.remove(infoType).remove("\n");
            base->received(ss);
        }
    }

}

TransferBase *TransferProxy::getPtr(const QString &infoType) {
   for(TransferBase* base : mTransVec) {
       if(base->transferPrefix() == infoType) {
           return base;
       }
   }
   return nullptr;
}

void TransferProxy::updataComInfo(bool isSendFlag, const QString &message) {
    QString time = QDateTime::currentDateTime().toString ("yyyy-MM-dd hh:mm:ss");
    if(isSendFlag) {
        comInfo = QString("%1\n%2 %3:\n%4").arg(comInfo).arg(mSendName).arg(time).arg(message);
    }else {
        comInfo = QString("%1\n%2 %3:\n%4").arg(comInfo).arg(mReceiveName).arg(time).arg(message);
    }
    //因为QML显示的是HTML 所以要把\n换成br
    comInfo = comInfo.replace("\n","<br>");
    emit comInfo_Notify();
}

TransferBase* TransferProxy::isHaveReceiving()
{
    //到这里说明没有找到 看看有没有正在运行中的任务，有的话就执行运行中的任务
    for(TransferBase* base : mTransVec) {
        if(base->isRunning()) {
            return base;
        }
    }
    return nullptr;
}

void TransferProxy::sendImage(QString path) {
    sendInfo(ImageTrans::prefix(), path);
}

void TransferProxy::sendMovie(QString path) {
    sendInfo(MovieTrans::prefix(), path);
}

void TransferProxy::sendTextInfo(QString info) {
    sendInfo(NormalChatTransfer::priefix(), info, true);
}

void TransferProxy::removeInfoDir() {
    QDir dir;
    QString removeDir = QDir::currentPath() + "/Temp";
    bool res = DelDir(removeDir);
    qDebug() << "remove res = " << res << "dir = " <<removeDir;
}
